package sge.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sge.beans.Disciplina;
import sge.beans.Fachada;
import sge.beans.Usuario;

public class DisciplinaServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String areaProfissional = req.getParameter("areaProfissional");
		String nomeCurso = req.getParameter("curso");
		String nomeDisciplina = req.getParameter("disciplina");

		Fachada fachada = Fachada.obterInstancia();
		Disciplina disciplina = new Disciplina();

		Long idArea = fachada.buscarIDArea(areaProfissional);
		disciplina.setIdArea(idArea);

		Long idCurso = fachada.buscarIDCurso(nomeCurso);
		disciplina.setIdCurso(idCurso);

		disciplina.setNomeDisciplina(nomeDisciplina);
		fachada.inserirDisciplina(disciplina);

		direcionarPagina(req, resp);

	}
	private void direcionarPagina(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Usuario usuario =  (Usuario) request.getSession().getAttribute("usuario");
		if(usuario.getTipo().equalsIgnoreCase("professor")){
			request.getRequestDispatcher(request.getContextPath()+"professor/feedback/cadastro_efetuado.jsp").forward(request, response);
			return;
		}
		if(usuario.getTipo().equalsIgnoreCase("coordenador")){
			request.getRequestDispatcher(request.getContextPath()+"coordenador/feedback/cadastro_efetuado.jsp").forward(request, response);
			return;
		}
		if(usuario.getTipo().equalsIgnoreCase("administrador")){
			request.getRequestDispatcher(request.getContextPath()+"administrador/feedback/cadastro_efetuado.jsp").forward(request, response);
			return;
		}		
	}
}
